# # Author ：Irene
#
Goods = [["1.iphone X",7399],["2.vivo X21",3598],["3.利用Python进行数据分析",72],["4.迪士尼滑板车", 179],["5.索尼无线蓝牙耳机", 1999]]
Havebuys = [] #已经购买的列表
salarytem = input("请输入你带多少钱来购物:")
if not salarytem.isdigit():
    exit()
salary = int(salarytem)
yuer = salary #余额
for i in Goods:
    print(i)

buyindex = ""
while True:
    buy = input("输入'q'退出,请输入购买物品的编号:")

    if buy.isdigit():
        buyindex = int(buy)
        if buyindex < 1 or buyindex > len(Goods):
            print("没有您想要的商品，请重新选择...")
            continue
    elif buy == "q":
        if len(Havebuys) < 1:
            print("你没有购买任何物品")
            exit()
        print("----------------购物车的清单-------------------")
        for j in Havebuys:
            print(j)
        print("你已经购买了以上的商品。剩余金额为：%f元" % yuer)
        exit()
    price = Goods[buyindex-1][1]
    if (price > yuer):
        print("\033您的余额不够，剩余：%f\033，请选择其他商品..." % yuer)
    elif (price <= yuer):
        yuer = yuer - price
        Havebuys.append(Goods[buyindex-1])
        print("购买成功，购买商品为：\n" + Goods[buyindex-1][0])

# from:zhichao

print("--------- Welcome to use ---------")
alipay_balance = int(input("Please enter the balance of Alipay:"))
shopping_list = []
while True:
    print("--------- Merchandise News ---------")
    goods_list = [["1.iphone X",7399],
                  ["2.vivo X21",3598],
                  ["3.利用Python进行数据分析",72],
                  ["4.迪士尼滑板车", 179],
                  ["5.索尼无线蓝牙耳机", 1999],
                  ["6.华为 Mate40",9800],
                  ["7.Nars散粉",430],
                  ["8.Airpods3",2800],
                  ["9.PUMA 卫衣",520],
                  ["10.纪梵希小羊皮口红",450]
            ]

    for item in goods_list:
        print(goods_list.index(item),item)

    user_choice = input("Please enter the item number you want or enter 'Q' to check the purchase information:")

    if user_chioce.isdigit():  # python isdigit()方法是 检测 字符串是否只由数字组成
        user_chioce = int(user_chioce)
        if user_chioce >= 0 and user_chioce <= len(goods_list):
            if alipay_balance >= goods_list[user_chioce][1]:
                shopping_list.append(goods_list[user_chioce])
                money02 = alipay_balance - goods_list[user_chioce][1]
                print("This item has been added for you，your balance is still there %s" % (money02))
                print(shopping_list)
            else:
                print("Your balance is insufficient.")
        else:
            print("This item you input does not exist.")
    elif user_choice == "Q":
        for item in shopping_list:
            print("You have bought%s" %(item[0]),"You have paid%s" %(item[1]))
        print("Your balance is still there%s" %(money02))
        print("You add %s to the shopping cart" %(shopping_list),"Your balance is still there{money}".format(money=money02))
        print()
        break
    else:
        print("You have typed in the wrong information.")

# from ：Network
# Auther：bentou

salary=input("salary:")  #用户输入可供支付的预期
goods=[("iphone 11",1200),("coffee",24),("ipad",3700)] #商品列表

'''
和enumerate()方法的功能一样，打印index下标
while True:
    for item in goods:
        print(goods.index(item),item)

    break
'''

shopping_car=[] #定义一个空列表，用来装需要放到购物车的商品
while True: #循环开始了
    for index,item in enumerate(goods): #enumerate()方法的作用：打印下标
        print(index,item) #每次循环都打印一下下标和商品
    choice=input("选择要买吗:") #本意是让用户选择下标（序号），用户退出的时候显示一共多少钱，钱不够的时候显示余额不足
    if choice.isdigit() : #当用户输入的值是0-9之间的阿拉伯数字
        choice=int(choice) #强制转换成数字
        if choice<len(goods) and choice>=0: #数字在食品列表范围内
            p_item=goods[choice] #让p_item等于用户选的那个商品元素
            if p_item[1]<= int(salary): #如果买得起，也就是售价之和小于工资
                shopping_car.append(p_item) #就把这个商品加入提前定义的空列表
                salary=int(salary)-p_item[1] #工资减去这个加购商品的价格
                print("Added %s into car,your current balance is %s" %(p_item,salary)) #告诉客户加了什么到购物车以及余额是多少
            else: #工资不够了，跳出循环
                print("Not enough!")
                break
        else:#商品列表没有这个商品
            print("no such goods")
    elif choice=="q":  #退出购物车了，告诉用户加了啥和余额多少
        print("------shopping car-------")
        for i in shopping_car:
            print(i)
        print("your current balance:",salary)
        break
    else: #输入不符规则
        print("invalid")


####
i=1
productsList={}
shopCar=[]
products=[['电脑',6900], ['苹果手机',8888], ['鼠标',59], ['Python教程',79]]
for x in products:
    productsList[i]=x
    i+=1
print('我们目前有以下商品')
while True:
    print(productsList)
    num=input('请输入你要购买得商品编号\n')
    if num.isdigit():
        num = int(num)
        if num<1 or num>4:
            print('对不起，编号有误,请重新选择')
            continue
        else:
            shopCar.append(productsList.get(num))
        continue
    else:
        print('您的输入有误....',end='')
    opera=input('商品已加入购物车，请按任意键继续购买，结算请按0\n')
    if opera=='0':
        shopCar=dict(shopCar)
        sum=0
        for x in shopCar.values():
            sum+=int(x)
        print('您购买的商品为')
        print(shopCar)
        print('总价为'+str(sum))
        break
